#include "junix.h"

int read_utmp (const char *filename, int *n_entries, struct utmp **utmp_buf)
{
	FILE *utmp;
	struct stat file_stats;
	size_t n_read;
	size_t size;
	struct utmp *buf;

	utmp = fopen (filename, "r");
	if (utmp == NULL)
		return 1;

	fstat (fileno (utmp), &file_stats);
	size = file_stats.st_size;
	if (size > 0)
		buf = (struct utmp *) xmalloc (size);
	else
	{
		fclose (utmp);
		return 1;
	}

	/* Use < instead of != in case the utmp just grew.  */
	n_read = fread (buf, 1, size, utmp);
	if (ferror (utmp) || fclose (utmp) == EOF
			|| n_read < size)
		return 1;

	*n_entries = size / sizeof(struct utmp);
	*utmp_buf = buf;

	return 0;
}

int main(int argc, char **argv)
{
	int size;
	struct utmp *putmp;
	if ( read_utmp("/var/run/utmp",&size, &putmp ) )
		err_quit("read_utmp error");

	int i=0;
	for(i=0; i<size; i++){
		printf("Line:%s\t", putmp->ut_user);
		printf("User:%s\t", putmp->ut_user);
		printf("Host:%s\t\n", putmp->ut_host);
		++ putmp;
	}

    exit(0);
}

